package com.shire.shire_interface.controller.phrase;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.phrase.WxPhrase;
import com.shire.shire_interface.model.phrase.WxPhraseExample;
import com.shire.shire_interface.model.user.SystemUser;
import com.shire.shire_interface.service.phrase.WxPhraseService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

//@Api(tags = "资料库")
@RestController
@RequestMapping("/phrase")
public class WxPhraseController extends BaseController {

    @Resource
    private WxPhraseService wxPhraseService;


//    @ApiOperation(value = "资料库列表 带分页 pageNo, pageSize")
    @ResponseBody
    @PostMapping("/getlimit")
    public Response getLimit(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();
        SystemUser user = (SystemUser)session.getAttribute("user");

        Integer pageNo = jsonObject.getInteger("pageNo");
        Integer pageSize = jsonObject.getInteger("pageSize");
        WxPhraseExample example = new WxPhraseExample();
        example.createCriteria().andCsidEqualTo(user.getId());
        int pageStart = getPageStartRow(pageNo, pageSize);
        example.setLimitStart(pageStart);
        example.setLimitSize(pageSize);
        example.setOrderByClause("id desc");
        try {

            if (!checkLoginUser(session)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            List<WxPhrase> wxPhrases = wxPhraseService.selectByExample(example);
            response.setData(wxPhrases);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

//    @ApiOperation(value = "资料库列表 csId, wxAccount")
    @ResponseBody
    @PostMapping("/get")
    public Response get(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();

        Integer csId = jsonObject.getInteger("csId");
        String wxAccount = jsonObject.getString("wxAccount");
        if (!checkLoginUserByCsId(session, csId)){
            response.error("请登录！");
            response.setCode(501);
            return response;
        }
        WxPhraseExample example = new WxPhraseExample();
        example.createCriteria().andCsidEqualTo(csId).andWxaccountwxidEqualTo(wxAccount);
        example.setOrderByClause("id desc");
        try {
            List<WxPhrase> wxPhrases = wxPhraseService.selectByExample(example);
            response.setData(wxPhrases);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

//    @ApiOperation(value = "添加")
    @ResponseBody
    @PostMapping("/add")
    public Response add(@RequestBody WxPhrase wxPhrase, HttpSession session){
        Response response = new Response();

        try {
            if (!checkLoginUserByCsId(session, wxPhrase.getCsid())){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            wxPhraseService.insert(wxPhrase);
            response.success("成功");
            response.setData(wxPhrase.getId());
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }

//    @ApiOperation(value = "删除 id")
    @ResponseBody
    @PostMapping("/del")
    public Response del(@RequestBody JSONObject jsonObject, HttpSession session){
        Response response = new Response();
        try {
            if (!checkLoginUser(session)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }
            Integer id = jsonObject.getInteger("id");
            wxPhraseService.delByPrimaryKey(id);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }
}
